from time import time
sys.path.insert(0,'../..')
from rmpoly import RPoly, Poly

try:
  n = int(sys.argv[1])
  h = int(sys.argv[2])
  fld = int(sys.argv[3])
  typ = int(sys.argv[4])
except:
  print '''prog n h fld typ
  p1 = (1 + a*t + b*t^2 + c*t^3 + d*t^4 + e*t^5 + O(t^h))^-1
  p2 = (1 + 2*a*t + 3*b*t^2 + 4*c*t^3 + 5*d*t^4 +6*e*t^5 + O(t^h))^-1
  bench (p1+p2)^-n

  fld:  0   QQ
        1   mpq
        2   GF(7)
        3   RealField(100)
  typ:  0   rmpoly
        1   PowerSeriesRing
        2   both
  '''
  sys.exit()

print sys.argv,

if fld == 0:
  field = QQ
  print 'using QQ',
elif fld == 1:
  from gmpy import mpq
  field = mpq
  print 'using mpq with RPoly and QQ with PowerSeriesRing',
elif fld == 2:
  field = GF(7)
  print 'using GF(7)',
else:
  field = RealField(100)
  print 'using RealField(100)',

if fld == 1:
  one = mpq('1')
  field1 = QQ
else:
  one = 1
  field1 = field

if typ == 0:
  rp = RPoly(['e','d','c','b','a','t'],10,field)
  e,d,c,b,a,t = rp.gens()
  p1 = (1 + a*t + b*t^2 + c*t^3 + d*t^4 + e*t^5).pow_trunc(-1,'t',h)
  p2 = (1 + 2*a*t + 3*b*t^2 + 4*c*t^3 + 5*d*t^4 +6*e*t^5).pow_trunc(-1,'t',h)
  p = p1 + p2
  t0 = time()
  p3 = p.pow_trunc(-n,'t',h)
  t1 = time()
  print '%.2f' %(t1-t0)
  #print 'len(p1)=',len(p1)
  #print p1
elif typ == 1:
  R.<a,b,c,d,e> = field1[]
  K.<t> = PowerSeriesRing(R)
  p1 = (1 + a*t + b*t^2 + c*t^3 + d*t^4 + e*t^5 + O(t^h))^-1
  p2 = (1 + 2*a*t + 3*b*t^2 + 4*c*t^3 + 5*d*t^4 +6*e*t^5 + O(t^h))^-1
  p = p1 + p2
  t0 = time()
  p3 = p^-n
  t1 = time()
  print '%.2f' %(t1-t0)
elif typ == 2:
  rp = RPoly(['e','d','c','b','a','t'],10,field)
  e,d,c,b,a,t = rp.gens()
  p1 = (1 + a*t + b*t^2 + c*t^3 + d*t^4 + e*t^5).pow_trunc(-1,'t',h)
  p2 = (1 + 2*a*t + 3*b*t^2 + 4*c*t^3 + 5*d*t^4 +6*e*t^5).pow_trunc(-1,'t',h)
  p = p1 + p2
  t0 = time()
  p3 = p.pow_trunc(-n,'t',h)
  t1 = time()
  s3 = str(p3)
  R.<a,b,c,d,e> = field1[]
  K.<t> = PowerSeriesRing(R)
  p1 = (1 + a*t + b*t^2 + c*t^3 + d*t^4 + e*t^5 + O(t^h))^-1
  p2 = (1 + 2*a*t + 3*b*t^2 + 4*c*t^3 + 5*d*t^4 +6*e*t^5 + O(t^h))^-1
  p = p1 + p2
  t2 = time()
  p4 = p^-n
  t3 = time()
  p3 = sage_eval(s3,{'a':a,'b':b,'c':c,'d':d,'e':e,'t':t})
  print p4 - p3
  print 'Rpoly:%.2f PowerSeriesRing:%.2f' %(t1-t0,t3-t1)

